package com.example.clsj.web;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.example.clsj.anno.Loggable;
import com.example.clsj.dto.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cy
 * @date 2024/1/5
 */
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Log log = LogFactory.get();

    /**
     * 自定义异常处理器，用于捕获并处理运行时的异常
     *
     * @param e 异常对象，用于获取错误信息
     * @return 返回一个包含错误代码和消息的JSON字符串
     */
    @ExceptionHandler(Exception.class)
    @Loggable
    public Result customException(Exception e) {
        // 记录错误日志，方便调试和追踪
        log.error(e.getMessage(), e);
        return Result.fail(900, e.getMessage());
    }
}
